package servlet;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.IStudentDao;
import dao.impl.StudentDaoImpl;
import pojo.Student;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/queryStudents")
public class StudentsServlet extends HttpServlet {
	/**
	 * 以json数组格式的字符串形式输出所查询到的学生信息
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//补充代码
		response.setContentType("text/html;charset=utf-8");
		String stuName = request.getParameter("stuName");
		IStudentDao studentDao=new StudentDaoImpl();
		List<Student> students = studentDao.findStudentsByName(stuName);
		Object o = JSON.toJSON(students);
		PrintWriter out = response.getWriter();
		System.out.println(o.toString());
		out.append(o.toString());

	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
